#include "bspTest.h"
#include "stdio.h"
#include "stdint-gcc.h"
#include "time.h"
#include "adc.h"
#include "usart.h"
#include "gpio.h"
#include "dac.h"


extern ADC_HandleTypeDef hadc1;
extern ADC_HandleTypeDef hadc2;
extern DAC_HandleTypeDef hdac;
// extern DMA_HandleTypeDef hdma_dac1;
// extern DMA_HandleTypeDef hdma_dac2;
// extern DAC_HandleTypeDef hdac;

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim7;
extern UART_HandleTypeDef huart1;

uint16_t adcValue[9];
uint16_t adc2_value[9];
uint16_t dac1_value = 10;
bspValue testBSP_value = {0};


const uint16_t CH_value[32] = 
{
	2448,2832,3186,3496,3751,3940,4057,4095,4057,3940,
	3751,3496,3186,2832,2448,2048,1648,1264,910,600,345,
	156,39,0,39,156,345,600,910,1264,1648,2048
};

void bsp_init(void)
{
    //timer start
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);

	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3);

    __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1, 0);
    __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2, 0);
    __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3, 0);
    testBSP_value.timPWM_Value[0] = 500;
    testBSP_value.timPWM_Value[1] = 1000;
    testBSP_value.timPWM_Value[2] = 1500;
    HAL_TIM_PWM_Start_IT(&htim1,TIM_CHANNEL_4);
	HAL_TIM_Base_Start(&htim1);
    //ENCODER TIMER START
    HAL_TIM_Base_Start(&htim2);
    HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_1|TIM_CHANNEL_2);
    __HAL_TIM_SET_COUNTER(&htim3,CNTZERO);
    //HALL TIMER START
    HAL_TIM_Base_Start(&htim3);
    HAL_TIM_Base_Start(&htim7);  //dac trigger timmer
    //adc start
    HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adcValue, 9);
    HAL_ADC_Start_DMA(&hadc2, (uint32_t *)adc2_value, 9);
    //dac start
    HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_1, (uint32_t *)CH_value, 32, DAC_ALIGN_12B_R);
    HAL_DAC_Start_DMA(&hdac,DAC_CHANNEL_2, (uint32_t *)CH_value, 32, DAC_ALIGN_12B_R);
}

void adc_dma_start(void)
{
   HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adcValue, 9); 
   HAL_ADC_Start_DMA(&hadc2, (uint32_t *)adc2_value, 9); 
}


int32_t get_timer2_encoder_cnt(void)
{
    testBSP_value.tim2_cnt += __HAL_TIM_GET_COUNTER(&htim2) - CNTZERO;
    __HAL_TIM_SET_COUNTER(&htim2, CNTZERO);
    if(testBSP_value.tim2_cnt < 0)
    {
        testBSP_value.tim2_cnt += ENCODERPLUSE;
    }
    testBSP_value.tim2_cnt = testBSP_value.tim2_cnt%ENCODERPLUSE;
    return testBSP_value.tim2_cnt;
}   


//after adc translate
void ADCDMAIRQHandler(void)
{
    HAL_ADC_Stop_DMA(&hadc1);
    testBSP_value.currentADC_Value[0] = (adcValue[0] + adcValue[3]  + adcValue[6])/2U;
    testBSP_value.currentADC_Value[1] = (adcValue[1] + adcValue[4]  + adcValue[7])/2U;
    testBSP_value.currentADC_Value[2] = (adcValue[2] + adcValue[5]  + adcValue[8])/2U;
    // get_timer2_encoder_cnt();

}

void adc2_irq_handler(void)
{
    HAL_ADC_Stop_DMA(&hadc2);
    testBSP_value.voltageADC_Value[0] = adc2_value[0];
    testBSP_value.voltageADC_Value[1] = adc2_value[1];
}



void dac_dmaIRQ_handler(void)
{
    // HAL_DAC_Stop_DMA(&hdac, DAC_CHANNEL_1);
    // HAL_DAC_Stop_DMA(&hdac, DAC_CHANNEL_2);
    // HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
    
}


void dac_test(void)
{
    
    static uint16_t count = 0;
    if(count == 4050)
    {
        count = 0;
    }
    count+=250;
    // uint16_t count_minus = 4096 - count;
    HAL_DAC_SetValue(&hdac,DAC_CHANNEL_2, DAC_ALIGN_12B_R, count); 
    HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
}












